require "Task"
require "TaskCategory/RandomTask"
require "WeightPool"

QNW_TS_WealthInDanger = QNW_TS_Task:derive("QNW_TS_WealthInDanger")

local this = QNW_TS_WealthInDanger
this.name = getText("IGUI_QNW_TaskSystem_Task_WealthInDanger_Name")
this.desc = getText("IGUI_QNW_TaskSystem_Task_WealthInDanger_Desc")
this.taskCategory = QNW_TS_RandomTask

function QNW_TS_WealthInDanger:isComplete()
    return self:isTime()
end

function QNW_TS_WealthInDanger:complete()
    if QNW_TS_TaskWindow.instance then
        QNW_TS_TaskWindow.instance:destroy()
    end
    QNW_TS_Task.complete(self)
    local player = getPlayer()
    local direction = ZombRand(4)
    if direction == 0 then
        for i = 1, 13 do
            QNW_TS_ClientCommand.addZombie(player:getX() + ZombRand(10, 18), player:getY(), 0, 1, 1)
        end
    end
    if direction == 1 then
        for i = 1, 13 do
            QNW_TS_ClientCommand.addZombie(player:getX() - ZombRand(10, 18), player:getY(), 0, 1, 1)
        end
    end
    if direction == 2 then
        for i = 1, 13 do
            QNW_TS_ClientCommand.addZombie(player:getX(), player:getY() + ZombRand(10, 18), 0, 1, 1)
        end
    end
    if direction == 3 then
        for i = 1, 13 do
            QNW_TS_ClientCommand.addZombie(player:getX(), player:getY() - ZombRand(10, 18), 0, 1, 1)
        end
    end
end

function QNW_TS_WealthInDanger:isAbandon()
    return self:isTime()
end

function QNW_TS_WealthInDanger:getProgress()
    return ""
end

function QNW_TS_WealthInDanger:registerClient(_player)
    _player:Say(getText("IGUI_QNW_TaskSystem_Get", self.name))
end

function QNW_TS_WealthInDanger:save()
    local save = {}
    save.Type = self.Type
    save.uuid = self.uuid
    save.time = self.time
    save.rewards = {}
    for k, v in pairs(self.rewards) do
        table.insert(save.rewards, v:save())
    end
    return save
end

function QNW_TS_WealthInDanger:load(_table)
    self.uuid = _table.uuid
    self.time = _table.time
    if _table.rewards then
        self:loadReward(_table.rewards)
    end
end

function QNW_TS_WealthInDanger:new()
    local o = QNW_TS_Task:new()
    setmetatable(o, self)
    self.__index = self

    o.uuid = getRandomUUID()
    o.time = getGameTime():getCalender():getTimeInMillis() + 60 * 60 * 2000  --原60 60 1000

    return o
end

function QNW_TS_WealthInDanger:initReward()
    QNW_TS_ClientCommand.randomReward(self, "height", 1)
end




--[[

require "Task"
require "TaskCategory/RandomTask"
require "WeightPool"

QNW_TS_WealthInDanger = QNW_TS_Task:derive("QNW_TS_WealthInDanger")

local this = QNW_TS_WealthInDanger
this.name = getText("IGUI_QNW_TaskSystem_Task_WealthInDanger_Name")
this.desc = getText("IGUI_QNW_TaskSystem_Task_WealthInDanger_Desc")
this.taskCategory = QNW_TS_RandomTask

function QNW_TS_WealthInDanger:isComplete()
    return self:isTime()
end

function QNW_TS_WealthInDanger:complete()
    if QNW_TS_TaskWindow.instance then
        QNW_TS_TaskWindow.instance:destroy()
    end
    QNW_TS_Task.complete(self)
    local player = getPlayer()
    player:getModData().ZBnum = nil
    local direction = ZombRand(4)
    if direction == 0 then
        for i = 1, 10 do
            addZombielocal(player, player:getX() + ZombRand(15, 30), player:getY(), 0, 1, 1)
        end
    end
    if direction == 1 then
        for i = 1, 10 do
            addZombielocal(player, player:getX() - ZombRand(15, 30), player:getY(), 0, 1, 1)
        end
    end
    if direction == 2 then
        for i = 1, 10 do
            addZombielocal(player, player:getX(), player:getY() + ZombRand(15, 30), 0, 1, 1)
        end
    end
    if direction == 3 then
        for i = 1, 10 do
            addZombielocal(player, player:getX(), player:getY() - ZombRand(15, 30), 0, 1, 1)
        end
    end
    local warn = getText("IGUI_WealthInDanger_Warn")
    processGeneralMessage(warn)
end

function QNW_TS_WealthInDanger:isAbandon()
    return self:isTime()
end

function QNW_TS_WealthInDanger:getProgress()
    return ""
end

function QNW_TS_WealthInDanger:registerClient(_player)
    _player:Say(getText("IGUI_QNW_TaskSystem_Get", self.name))
end

function QNW_TS_WealthInDanger:save()
    local save = {}
    save.Type = self.Type
    save.uuid = self.uuid
    save.time = self.time
    save.rewards = {}
    for k, v in pairs(self.rewards) do
        table.insert(save.rewards, v:save())
    end
    return save
end

function QNW_TS_WealthInDanger:load(_table)
    self.uuid = _table.uuid
    self.time = _table.time
    if _table.rewards then
        self:loadReward(_table.rewards)
    end
end

function QNW_TS_WealthInDanger:new()
    local o = QNW_TS_Task:new()
    setmetatable(o, self)
    self.__index = self

    o.uuid = getRandomUUID()
    o.time = getGameTime():getCalender():getTimeInMillis() + 60 * 60 * 6500

    return o
end

function QNW_TS_WealthInDanger:initReward()
    QNW_TS_ClientCommand.randomReward(self, "premium", 3)
end

local rewardlist = {"Base.SuiPian", "Base.CreditCard", "Base.SuiPian"}
local rewardCountlist = {8, 6, 4}

function addZombielocal(_player, _x, _y, _z, _count, _type)
    if not _player:getModData().ZBnum or _player:getModData().ZBnum == nil or #_player:getModData().ZBnum == 0 then
        _player:getModData().ZBnum = {}
    end
    if not _count then
        _count = 1
    end
    local list = addZombiesInOutfit(_x, _y, _z, _count, nil, 50)
    if _type and _type == 1 then
        for i = 0, list:size() - 1 do
            local zombie = list:get(i)
            if _player:getModData().ZBnum then
                local num = #_player:getModData().ZBnum
                _player:getModData().ZBnum[num + 1] = zombie
            end
            zombie:setWalkType("sprint1")
            zombie:pathToCharacter(_player)
        end
    end
end

function checkZombie(Zombie)
    local playerObj = getPlayer()
    local ZombieTemp = Zombie
    Zombie = nil
    if playerObj:getModData().ZBnum ~= nil then
        if #playerObj:getModData().ZBnum ~= 0 then
            for i = 0, #playerObj:getModData().ZBnum do
                local zombietodelete = playerObj:getModData().ZBnum[i]
                if zombietodelete == ZombieTemp then
                    table.remove(playerObj:getModData().ZBnum, i)
                    local temp = tostring(#playerObj:getModData().ZBnum)
                    playerObj:Say(temp)

                end
                if #playerObj:getModData().ZBnum == 0 then
                    local num = ZombRand(1, 3)
                    local Type = rewardlist[num]
                    local Count = rewardCountlist[num]
                    playerObj:getInventory():AddItems(Type, Count)
                    local item = playerObj:getInventory():FindAndReturn(Type)
                    local message = item:getName() .. "*" .. tostring(Count)
                    playerObj:Say(message)
                end
            end
        end
    end
end

Events.OnZombieDead.Add(checkZombie)
]]